home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / Pair / Pair.h < prev   
C/C++ Source or Header  |  1992-05-15  |  7KB  |  194 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. //
  13. // Created: JCB 06/22/89 -- Design and implementation
  14. // Updated: LGO 08/15/89 -- Removed operator>>
  15. // Updated: MBN 08/20/89 -- Changed usage of template to reflect new syntax
  16. // Updated: LGO 10/15/89 -- Added first & second
  17. // Updated: LGO 10/15/89 -- Made get_first and get_second const 
  18. // Updated: MBN 12/15/89 -- Added optional argument to set_compare method
  19. // Updated: MJF 07/31/90 -- Added terse print
  20. // Updated: DLS 03/27/91 -- New lite version
  21. //
  22. // The parameterized Pair<KeyT, ValueT> class implements an association between
  23. // one object  and another. The objects  may  be of  different  types, with the
  24. // first representing the  "key" of the  pair  and the second  representing the
  25. // "value" of the pair. The Pair<KeyT, ValueT> class is used by the Association
  26. // class to  implement  an a-list,  that  is a  vector   of pairs of associated
  27. // values.
  28. //
  29. // The Pair<KeyT,  ValueT>  class is relatively  simple,  having only two  data
  30. // slots. The first slot retains the key of the pair,  and the second holds the
  31. // value.  There are three constructors for the class: a constructor that takes
  32. // no arguments and creates an empty pair object; a  constructor that takes two
  33. // initial values,  one  each for the  key  and  the value of  the  pair; and a
  34. // constructor that takes a refernece to another Pair<KeyT, ValueT>  object and
  35. // copies the values.
  36. //
  37. // Methods are provided to get and set the value of  the key and value objects,
  38. // assign one Pair<KeyT, ValueT> object to anothher,  and test for equality and
  39. // inequality between  two pairs. Finally  the output operator is overloaded to
  40. // provide a means to display the value of a pair object.
  41. //
  42.  
  43. #ifndef PAIRH
  44. #define PAIRH
  45.  
  46. #ifndef STREAMH            // If the Stream support not yet defined,
  47. #if defined(DOS) || defined(M_XENIX)
  48. #include <stream.hxx>           // include the Stream class header file
  49. #else
  50. #include <stream.h>        // include the Stream class header file
  51. #endif
  52. #define STREAMH
  53. #endif
  54.  
  55. #ifndef MISCELANEOUSH        // If we have not included this file,
  56. #include <misc.h>        // include miscelaneous useful definitions.
  57. #endif
  58.  
  59.  
  60. template <class T1, class T2> CoolPair {
  61.   class CoolPair<T1,T2>;        // Forward reference class name
  62.   typedef Boolean (*CoolPair<T1,T2>_Compare) (const CoolPair<T1,T2>&,
  63.                            const CoolPair<T1,T2>&);
  64. }
  65.  
  66.  
  67. template <class T1, class T2>
  68. class CoolPair<T1,T2> {
  69. public:
  70.   CoolPair<T1,T2> ();                // CoolPair p;
  71.   CoolPair<T1,T2> (const T1&, const T2&);    // CoolPair p = (foo,bar);
  72.   CoolPair<T1,T2> (const CoolPair<T1,T2>&);    // CoolPair p1 = p2;
  73.   ~CoolPair<T1,T2> ();                // Destructor
  74.   
  75.   inline const T1& get_first () const;        // Get first element of pair
  76.   inline const T2& get_second () const;        // Get second element of pair
  77.   inline void set_first (const T1&);        // Set first element of pair
  78.   inline void set_second (const T2&);        // Set second element of pair
  79.   inline T1& first ();                // Get reference to 1st element
  80.   inline T2& second ();                // Get reference to 2nd element
  81.   
  82.   CoolPair<T1,T2>& operator= (const CoolPair<T1,T2>&); // Assignment p1 = p2;
  83.   
  84.   inline Boolean operator== (const CoolPair<T1,T2>&) const; // is equal
  85.   inline Boolean operator!= (const CoolPair<T1,T2>&) const; // is not equal
  86.   
  87.   void set_compare(CoolPair<T1,T2>_Compare = NULL); // Set compare function
  88.   
  89.   friend ostream& operator<< (ostream&, const CoolPair<T1,T2>&); // Output operator
  90.   inline friend ostream& operator<< (ostream&, const CoolPair<T1,T2>*);
  91.   
  92.   void print(ostream&);                // terse print
  93.  
  94. private:
  95.   T1 firstd;                    // First data slot
  96.   T2 secondd;                    // Second data slot
  97.   static CoolPair<T1,T2>_Compare compare_s;    // Pointer operator== function
  98.   friend Boolean CoolPair<T1,T2>_is_data_equal (const CoolPair<T1,T2>&,
  99.                         const CoolPair<T1,T2>&); 
  100. };
  101.  
  102.  
  103. // get_first -- Return the first element of the pair
  104. // Input:       None
  105. // Output:      const Reference to the first element of the pair
  106.  
  107. template<class T1, class T2> 
  108. inline const T1& CoolPair<T1,T2>::get_first () const {
  109.   return this->firstd;
  110. }
  111.  
  112.  
  113. // get_second -- Return the second element of the pair
  114. // Input:        None.
  115. // Output:       const Reference to the second element of the pair
  116.  
  117. template<class T1, class T2> 
  118. inline const T2& CoolPair<T1,T2>::get_second () const {
  119.   return this->secondd;
  120. }
  121.  
  122.  
  123. // first -- Return the first element of the pair
  124. // Input:       None
  125. // Output:      Reference to the first element of the pair
  126.  
  127. template<class T1, class T2> 
  128. inline T1& CoolPair<T1,T2>::first () {
  129.   return this->firstd;
  130. }
  131.  
  132.  
  133. // second -- Return the second element of the pair
  134. // Input:        None.
  135. // Output:       Reference to the second element of the pair
  136.  
  137. template<class T1, class T2> 
  138. inline T2& CoolPair<T1,T2>::second () {
  139.   return this->secondd;
  140. }
  141.  
  142.  
  143. // set_first -- Set the first element of the pair
  144. // Input:       Reference to a first element value
  145. // Output:      None.
  146.  
  147. template<class T1, class T2> 
  148. inline void CoolPair<T1,T2>::set_first (const T1& first) {
  149.   this->firstd = first;
  150. }
  151.  
  152.  
  153. // set_second -- Set the first element of the pair
  154. // Input:        Reference to a second element value
  155. // Output:       None.
  156.  
  157. template<class T1, class T2> 
  158. inline void CoolPair<T1,T2>::set_second (const T2& second) {
  159.   this->secondd = second;
  160. }
  161.  
  162.  
  163. // operator== -- Return TRUE if this pair and another specified are equal
  164. // Input:        Reference to a pair
  165. // Output:       TRUE or FALSE
  166.  
  167. template<class T1, class T2> 
  168. inline Boolean CoolPair<T1,T2>::operator== (const CoolPair<T1,T2>& p) const {
  169.   return (*this->compare_s)(*this, p);
  170. }
  171.  
  172.  
  173. // operator!= -- Return TRUE if this pair and another specified are not equal
  174. // Input:        Reference to a pair
  175. // Output:       TRUE or FALSE
  176.  
  177. template<class T1, class T2> 
  178. inline Boolean CoolPair<T1,T2>::operator!= (const CoolPair<T1,T2>& p) const {
  179.   return !(*this->compare_s)(*this, p);
  180. }
  181.  
  182.  
  183. // operator<< -- Overload output operator for the pair class
  184. // Input:        Pointer to a pair object, reference to an output stream
  185. // Output:       Reference to an output stream
  186.  
  187. template<class T1, class T2> CoolPair {
  188. inline ostream& operator<< (ostream& os, const CoolPair<T1,T2>* p) {
  189.   return operator<< (os, *p);
  190. }}
  191.  
  192.  
  193. #endif                        // End #ifndef PAIRH
  194.